//
//  WLPayModel.m
//  wangWLPay
//
//  Created by scjh on 15/2/8.
//  Copyright (c) 2015年 wangliang0. All rights reserved.
//

#import "WLPayModel.h"

#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

//合作身份者id，以2088开头的16位纯数字
#define PartnerID @"2088711915638401"

//收款支付宝账号
#define SellerID  @"jinyanxiao@scjhapp.com"

//安全校验码（MD5）密钥，以数字和字母组成的32位字符
#define MD5_KEY @"vkjf2jcens76m2uj92vtwqnx6uo1dfay"

//商户私钥，自助生成
#define PartnerPrivKey @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMUBaP5yKpdHO6D6WqvEGp9s12AN2LV4DaDqOb4ae80fe61T2EQ/ymI7UwMfzQzV1OgrDxP/LWNyWkXOBshaULieuppUCjiVk0tiC/lQ2VocjPid8lDo/06uqqMYP/tsSUQ1EkjJGUdEUEcPBBGai6PEHbI6bEee7oiYVhSqnOiRAgMBAAECgYAaBGCoUc18IwUl8kotKv3VFhuvbMS02oD6QlST3Y2B/mln7429HauNo4YIAJgk3Mx8SMt5+ZY17vErDNVqejr0m2m5FsLLV8Sq3uS3fX+Fl+qeVl11i+eTKd81Wz1UblCzCV9rym+6ORMiVjL4bott1oCTc/1dKLs4msobOHXODQJBAOrJc1W9pQYYIw9eCWFyuvhEbJNiyHBZA4D7xxXAhBH4Tt/hqCBbfDBzE0aPi0eIPIRcZps5V82rwYajtiS0EysCQQDWzhZTk6ZinEu9vEV4HH7rA3loTN3DqBD8GU4M46DF2+gb8UQc6Nb5UmEWS85rvq6BUV9dCauyIhz+ZAQdd8UzAkEA2jJBt4s5lEJWQu/Xs/KWBChC525EbPSspvBWgsaZdsHXO4VRZFDhR32KimbYv6/C3hepW9Sc3w0iG2iF2/JmZQJAED2WrIz+SHhetaK1ceZdpjEmDb+6CJ/9YMIdB5ZyQmNlAi/UoYbtMf6Lfp8OpVDOatKXLjab96Tiok03jm7OxwJBALt42/fgFK4+XSs2Q9KIKd2UB/ZwvS9uSf/X6twkIKw5AjPwp01bWJ6a0mNm2uCGpUaIiK9LINJvyAF06yU/XME="


//支付宝公钥
#define AlipayPubKey @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB"


@implementation PayProduct

@end

@implementation WLPayModel



+ (void) payModelProudct:(PayProduct *)payProudct withBlock:(void(^)(NSDictionary * resultDictionary))sucessBlock {
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = PartnerID;
    NSString *seller = SellerID;
    NSString *privateKey = PartnerPrivKey;
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
//    if ([partner length] == 0 ||
//        [seller length] == 0 ||
//        [privateKey length] == 0)
//    {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
//                                                        message:@"缺少partner或者seller或者私钥。"
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:nil];
//        [alert show];
//        return;
//    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = payProudct.orderID; //订单ID（由商家自行制定）
    order.productName = payProudct.name; //商品标题
    order.productDescription = payProudct.desc; //商品描述
    order.amount = payProudct.price; //商品价格
    order.notifyURL = [NSString stringWithFormat:@"%@/user/orderPay",kMainUrl]; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"com.DSX";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            
            sucessBlock(resultDic);
            
        }];
        
    }
    
}

+ (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand(time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}


@end
